package com.elasticsearchtemplate.service;

import com.elasticsearchtemplate.dao.EmpElasticTemplateDao;
import com.elasticsearchtemplate.entity.Emp;
import org.intellij.lang.annotations.Language;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpElasticTemplateServiceImpl implements EmpElasticTemplateService {

    public static final String INDEX_NAME = "emps";
    @Language("JSON")
    public static final String MAPPING_JSON = "{\n" +
            "  \"properties\": {\n" +
            "    \"name\": {\n" +
            "      \"type\": \"text\",\n" +
            "      \"analyzer\": \"ik_max_word\"\n" +
            "    },\n" +
            "    \"age\": {\n" +
            "      \"type\": \"integer\"\n" +
            "    },\n" +
            "    \"birthday\": {\n" +
            "      \"type\": \"date\",\n" +
            "      \"format\": \"yyyy-MM-dd\"\n" +
            "    },\n" +
            "    \"birthplace\": {\n" +
            "      \"type\": \"text\",\n" +
            "      \"analyzer\": \"ik_max_word\"\n" +
            "    }\n" +
            "  }\n" +
            "}";



    @Autowired
    private EmpElasticTemplateDao dao;

    @Override
    public void createIndexAndMapping() {
        dao.createIndexAndMapping(INDEX_NAME,MAPPING_JSON);
    }

    @Override
    public void insertDocument(Emp emp) {
        dao.insertDocument(INDEX_NAME,emp);
    }

    @Override
    public List<Emp> findAllByName(String name){
        return dao.findAllByName(INDEX_NAME,name);
    }

}
